package com.ax.service.impl;

import com.ax.dto.EmployeeDTO;
import com.ax.exception.BaseException;
import com.ax.result.PageResult;
import com.ax.constant.StatusConstant;
import com.ax.dto.EmployeePageQueryDTO;
import com.ax.entity.Employee;
import com.ax.mapper.EmployeeMapper;
import com.ax.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PasswordEncoder passwordEncoder;


    public PageResult pageQueryEmployee(EmployeePageQueryDTO employeePageQueryDTO) {
        //page与pagesize
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        //分页查询
        Page<Employee> pageQuery =employeeMapper.pageQuery(employeePageQueryDTO);
        //创建自定义分页结果对象
        PageResult pageResult = new PageResult();
        //返回符合条件的总记录数
        pageResult.setTotal(pageQuery.getTotal());
        //返回当前页的实际数据列表
        pageResult.setRecords(pageQuery.getResult());

        return pageResult;
    }

    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);

        employeeMapper.update(employee);
    }

    /**
     * 新增员工
     * @param employeeDTO
     */
    public void save(EmployeeDTO employeeDTO) {
        //转换DTO为实体对象
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setStatus(StatusConstant.ENABLE);

//        employee.setPassword(passwordEncoder.encode("123456".getBytes()));
        //String defaultPassword = "123456";
        String rawPassword = employeeDTO.getPassword();
        // 使用BCrypt加密密码
        String encodedPassword = passwordEncoder.encode(rawPassword);
        employee.setPassword(encodedPassword);

        employeeMapper.insert(employee);
    }

    /**
     * 注册员工
     * @param employeeDTO
     */
//    private Long id;
//
//    private String username;
//
//    private String password;
//
//    private String name;
//
//    private String phone;
//
//    private String sex;
//
//    private String idNumber;
    public void register(EmployeeDTO employeeDTO){
        if (employeeDTO == null) {
            throw new IllegalArgumentException("注册信息不能为空");
        }
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", employeeDTO.getUsername());
        Long count = employeeMapper.selectCount(queryWrapper);
        if (count != null && count > 0) {
            throw new BaseException("用户名已被占用");
        }
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        String rawPassword = employeeDTO.getPassword();
        // 使用BCrypt加密密码
        String encodedPassword = passwordEncoder.encode(rawPassword);
        employee.setPassword(encodedPassword);
        employeeMapper.insert(employee);
    }
}
